﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpICTCLAS;

namespace DisposeText
{
    /// <summary>
    /// 字符串处理类
    /// </summary>
    public class StringSolve
    {
        private string[] sentenceSplit ={ "。", "，", ",", "！", "……", "!", "？", "?", "；", "\n\r", "\r\n", "\r", "\n" };

        private WordSegmentSolve wordSegmentSolve;
        private WordBook wordBook;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="path">ICTCLAS数据所在位置</param>
        public StringSolve(string path)
        {
            string dictPath = path;
            wordSegmentSolve = new WordSegmentSolve(dictPath+@"/Data/", 2);
            wordBook = new WordBook(dictPath);
        }

        /// <summary>
        /// 获得待处理文本中某一特征的所有词汇
        /// </summary>
        /// <param name="text">待处理文本</param>
        /// <param name="wordFeature">特征</param>
        /// <returns></returns>
        public List<Word> GetWordsFromTextByFeature(string text,WordFeature wordFeature)
        {
            List<Word> words = new List<Word>();

            foreach (Word word in SegmentText(text))
            {
                if (wordBook.GetFeatureByWord(word.Value) == wordFeature)
                {
                    words.Add(word);
                }
            }

            return words;
        }

        /// <summary>
        /// 获得待处理文本中某一特征的所有词汇的个数
        /// </summary>
        /// <param name="text">待处理文本</param>
        /// <param name="wordFeature">特征</param>
        /// <returns></returns>
        public int GetWordsLengthFromTextByFeature(string text, WordFeature wordFeature)
        {
            return GetWordsFromTextByFeature(text, wordFeature).Count;
        }

        /// <summary>
        /// 将text文本中具有消极意义否定意义和积极意义的单词分别加以不同的颜色标签
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public string ProcessTextWithColors(string text)
        {
            List<Word> words = SegmentText(text);

            ProcessWordsByFeature(words, WordFeature.Positive,
                                   @"<font color=red>", @"</font>");

            ProcessWordsByFeature(words, WordFeature.Negative,
                                   @"<font color=green>", @"</font>");
            ProcessWordsByFeature(words, WordFeature.Opposite,
                                   @"<font color=yellow>", @"</font>");

            return string.Join(" ", words);
        }

        /// <summary>
        /// 将单词集中具有某一特征的单词加以特定的标签
        /// </summary>
        /// <param name="words"></param>
        /// <param name="wordFeature"></param>
        /// <param name="frontLabel"></param>
        /// <param name="rearLabel"></param>
        private void ProcessWordsByFeature(List<Word> words,WordFeature wordFeature,
                                                 string frontLabel, string rearLabel)
        {
            for (int i = 0; i < words.Count; i++)
            {
                if (wordBook.GetFeatureByWord(words[i].ToString()) == wordFeature)
                {
                    words[i].Value = frontLabel + words[i].Value + rearLabel;
                }
            }

            //return words;
        }

        /// <summary>
        /// 分析文本
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        private List<Word> SegmentText(string text)
        {
            List<string> sentences = SegmentSentences(text);

            List<Word> words = new List<Word>();

            foreach (string sentence in sentences)
            {
                words.AddRange(SegmentSentence(sentence));
            }

            return words;
        }

        /// <summary>
        /// 分句
        /// </summary>
        /// <param name="sentences"></param>
        /// <returns></returns>
        private List<string> SegmentSentences(string sentences)
        {
            return new List<string>(
                sentences.Split(sentenceSplit, StringSplitOptions.RemoveEmptyEntries)
                );
        }

        /// <summary>
        /// 分词
        /// </summary>
        /// <param name="sentence"></param>
        /// <returns></returns>
        private List<Word> SegmentSentence(string sentence)
        {
            List<WordResult[]> result = wordSegmentSolve.Segment(@"" + sentence + "");
            List<Word> newResult = new List<Word>();

            for (int i = 0; i < result.Count; i++)
            {
                for (int j = 1; j < result[i].Length - 1; j++)
                {
                    string value = result[i][j].sWord;
                    //string POS = Utility.GetPOSString(result[i][j].nPOS);

                    //newResult.Add(new Word(value, POS));
                    newResult.Add(new Word(value));
                }
            }

            return newResult;
        }
    }
}
